#pragma ident "$Id: IFCBData.cpp 2897 2012-08-10 20:08:46Z shjzhang $"

/**
 * @file IFCBData.cpp
 * Encapsulate satellite ifcb file data, including I/O
 */

//============================================================================
//
//  Shoujian Zhang, Wuhan University, 2012
//
//============================================================================

#include "IFCBData.hpp"
#include "IFCBStream.hpp"
#include "StringUtils.hpp"
#include "TimeString.hpp"
#include "CivilTime.hpp"

using namespace gpstk::StringUtils;
using namespace std;

#define debug 0
namespace gpstk
{

   void IFCBData::reallyPutRecord(FFStream& ffs) const
      noexcept(false)
   {
         // cast the stream to be an IFCBData
      IFCBStream& strm = dynamic_cast<IFCBStream&>(ffs);

      int i;
      string line;

      line = dataType;
      line += string(1,' ');

      line += string(1,sat.systemChar());
      line += rightJustify(asString(sat.id),2);
      if(line[4] == ' ') line[4] = '0';
      line += string(1,' ');

      line += printTime(time,"%4Y %02m %02d %02H %02M %9.6f");

      line += asString(ifcb, 10);
      line += string(1,' ');

      strm << line << endl;
      strm.lineNumber++;

   }  // end reallyPutRecord()

   void IFCBData::reallyGetRecord(FFStream& ffs)
      noexcept(false)
   {
         // cast the stream to be an IFCBData
      IFCBStream& strm = dynamic_cast<IFCBStream&>(ffs);

      string line;
      strm.formattedGetLine(line,true);      // true means 'expect possible EOF'
      stripTrailing(line);
      if(line.length() < 44)
      {
         FFStreamError e("Short line : " + line);
         GPSTK_THROW(e);
      }

      if(debug)
      cout << "Data Line: /" << line << "/" << endl;

      dataType = line.substr(0,2);
      string sys = line.substr(3,1);
      int prn(asInt(line.substr(4,2)));

      if(sys[0] == 'G')
      {
         sat = SatID(prn,SatelliteSystem::GPS);
      }
      else if(sys[0] == 'R')
      {
         sat = SatID(prn,SatelliteSystem::GLONASS);
      }
      else
      {
         FFStreamError e("Invalid sat : /" + sat.toString() + "/");
         GPSTK_THROW(e);
      }
/*
      CivilTime tc( asInt(line.substr( 8,4)),
                    asInt(line.substr(12,3)),
                    asInt(line.substr(15,3)),
                    asInt(line.substr(18,3)),
                    asInt(line.substr(21,3)),
                    asDouble(line.substr(24,10)),
                    TimeSystem::Any);
*/

      CivilTime tc( asInt(line.substr( 7,4)),
                    asInt(line.substr(11,3)),
                    asInt(line.substr(13,3)),
                    asInt(line.substr(17,3)),
                    asInt(line.substr(20,3)),
                    asDouble(line.substr(23,10)),
                    TimeSystem::Any);


        // Convert to CommonTime
      time = tc.convertToCommonTime();

      ifcb = asDouble(line.substr(34,10));

      if(debug)
      cout << "dataType:" << dataType << " sys:" << sys << " prn:" << prn
           << "time:" << tc << " ifcb:" << ifcb << endl;


   }   // end reallyGetRecord()

   void IFCBData::dump(ostream& s) const throw()
   {
      // dump record type, sat id / site, current epoch, and data
      s << " " << dataType;

      s << " " << sat.toString();

      s << " " << printTime(time,"%Y/%02m/%02d %2H:%02M:%06.3f = %F/%10.3g %P");

      s << scientific << setprecision(3)
         << " " << setw(8) << ifcb;

      s << endl;

   }  // end dump()

} // namespace

//------------------------------------------------------------------------------------
